if (typeof (Giamua) == 'undefined')
    var Giamua = {};

Giamua.SimpleDialog = function(opttions) {
    var defaults = {
        dialog_id: 'simple_dialog',
        width: 500,
        height: '',
        position: 'fixed',
        waiting_message: '<p class="dialog_loading">Đang tải dữ liệu...</p>'
    };
    var opts = jQuery.extend({}, defaults, opttions);
    var ie6 = ($.browser.msie && $.browser.version < 7);
    var dialog;
    var dialog_content;

    var init = function() {
        dialog = $('#' + opts.dialog_id);
        if (dialog.length == 0) {
            var dialog_html = '<div id="' + opts.dialog_id + '" style="display:none;"> \
				                          <div class="popup"> \
				                            <table> \
				                              <tbody> \
				                                <tr> \
				                                  <td class="tl"/><td class="b"/><td class="tr"/> \
				                                </tr> \
				                                <tr> \
				                                  <td class="b"/> \
				                                  <td class="body"> \
				                                    <div class="simple-dialog-content"> \
				                                    </div> \
				                                  </td> \
				                                  <td class="b"/> \
				                                </tr> \
				                                <tr> \
				                                  <td class="bl"/><td class="b"/><td class="br"/> \
				                                </tr> \
				                              </tbody> \
				                            </table> \
				                          </div> \
				                        </div>';

            dialog = $(dialog_html);
            dialog_content = dialog.find('div.simple-dialog-content');

            $(document.body).append(dialog);
        }
        else {
            dialog_content = dialog.find('div.simple-dialog-content');
        }

        dialog_content.empty().html(opts.waiting_message);
        dialog.css({
            'width': opts.width + 'px',
            'marginLeft': '-' + opts.width / 2 + 'px'
            /*'position': opts.position*/
        });
    };

    init();

    return {
        show: function() {
            if (dialog == null)
                init();

            if (opts.position == 'fixed') {
                dialog.attr('class', 'fixed');
            } else {
                dialog.attr('class', '');
            }

            dialog.fadeIn(300);
        },

        hide: function() {
            dialog.fadeOut(300, function() {
                dialog_content.empty().html(opts.waiting_message);
            });
        },

        setContent: function(html) {
            dialog_content.empty().html(html);
        },

        resetContent: function() {
            dialog_content.empty().html(opts.waiting_message);
        },

        width: function(width) {
            dialog.css({ 'width': width + 'px', 'marginLeft': '-' + width / 2 + 'px' });
        },

        resetSize: function() {
            dialog.css({ 'width': opts.width + 'px', 'height': opts.height + 'px', 'marginLeft': '-' + opts.width / 2 + 'px' });
        }
    };
};

Giamua.RegisterMailDialog = function () {
    var dialog = new Giamua.SimpleDialog({ width: 400, height: 230, position: 'fixed' });
    var register_content = '<div style="background-color:#fff;"> \
                                <iframe width="380px" height="210px" src="/subscribe.aspx" frameBorder="0" scrolling="no"></iframe> \
                                <div class="action-bar"><a href="javascript:void(0);" class="close"><img src="/images/close-icon.gif" alt="Đóng popup" style="border: 0px;"/></a></div> \
                            </div>';
    var requestData = function() {
        $.ajax({
            url: '/services/sendmail.aspx',
            data: '',
            dataType: 'html',
            type: 'post',
            async: true,
            success: function(results) {
                dialog.setContent(results);

                if (typeof dialogCallback !== undefined)
                    dialogCallback();
            },
            error: function(xhr) {
                if (xhr.status == 403) {
                    alert('Vui lòng <a herf="/login">đăng nhập</a> để sử dụng chức năng này!');
                } else if (xhr.status == 400) {
                    alert('Yêu cầu không hợp lệ!');
                }
                else {
                    alert('Xảy ra lỗi khi thực thi!');
                }
                dialog.hide();
            }
        });
    };

    var _show = function() {
        dialog.resetSize();
        dialog.setContent(register_content);
        dialog.show();       
    };

	var _save = function() {
		 requestData();
	};

    var _hide = function() {
        dialog.hide();
    }

    return {        
        show: function() {
            _show();
        },
		save: function() {
            _save();
        },
        close: function() {
            _hide();
        }
    }
};


Giamua.CouponLinkDialog = function() {
    var dialog = new Giamua.SimpleDialog({ width: 600, height: 330, position: 'fixed' });
    var requestData = function() {
        $.ajax({
            url: '/services/save.aspx',
            data: '',
            dataType: 'html',
            type: 'post',
            async: true,
            success: function(results) {
                dialog.setContent(results);

                if (typeof dialogCallback !== undefined)
                    dialogCallback();
            },
            error: function(xhr) {
                if (xhr.status == 403) {
                    alert('Vui lòng <a herf="/login">đăng nhập</a> để sử dụng chức năng này!');
                } else if (xhr.status == 400) {
                    alert('Yêu cầu không hợp lệ!');
                }
                else {
                    alert('Xảy ra lỗi khi thực thi!');
                }
                dialog.hide();
            }
        });
    };

    var _show = function() {
        dialog.resetSize();
        dialog.setContent($('#coupon-link-div').html());
        dialog.show();
    };

    var _save = function() {
        requestData();
    };

    var _hide = function() {
        dialog.hide();
    }

    return {
        show: function() {
            _show();
        },
        save: function() {
            _save();
        },
        close: function() {
            _hide();
        }
    }
};

Giamua.LoginDialog = function() {
    var dialog = new Giamua.SimpleDialog({ width: 410, height: 200, position: 'fixed' });
    
    var login_content = '<div style="background-color:#fff;"> \
                            <iframe width="390px" height="180px" src="/logindialog.aspx" frameBorder="0" scrolling="no"></iframe> \
                            <div class="action-bar"><a href="javascript:void(0);" class="close"><img src="/images/close-icon.gif" alt="Đóng popup" style="border: 0px;"/></a></div> \
                        </div>';

    var _show = function() {
        dialog.resetSize();
        dialog.setContent(login_content);
        dialog.show();
    };

    var _hide = function() {
        dialog.hide();
    };

    return {
        show: function() {
            _show();
        },

        close: function() {
            _hide();
        }
    }
};

//var mailDialog = new Giamua.RegisterMailDialog();

Giamua.FloatFlash = function(options) {
    var _defaults = {
        controlID: 'float_qc',
        offsetX: '5',
        offsetY: '5',
        controlHTML: '',
        swfFile: '',
        url: '',
        width: '0',
        height: '0',
        cookie_name: 'flash_ads'
    };

    var _opts = jQuery.extend({}, _defaults, options);
    var _window = jQuery(window);
    var _control, _cssFixedSupport;

    var _keepFixed = function() {
        var controlX = _window.scrollLeft() + _window.width() - _control.width() - _opts.offsetX;
        var controlY = _window.scrollTop() + _window.height() - _control.height() - _opts.offsetY;
        _control.css({ left: controlX + 'px', top: controlY + 'px' });
    };

    var _adjustContainer = function() {
        var scrolltop = _window.scrollTop();
        if (!_cssFixedSupport) {
            _keepFixed();
        }
    };

    var _init = function() {
        // dont show ads if user closed it
        if (!_canShow()) {
            return;
        }

        _opts.controlHTML = '<div class="qc-float-inner" style="position:relative;width:' + _opts.width + 'px;height:' + _opts.height + 'px"> \
                                <div class="qc-overlay" style=\"position:absolute;opacity:0;visibility:visible;background-color:#fff;top:0px;left:0px;width:' + _opts.width + 'px;height:' + _opts.height + 'px;cursor:pointer\"></div>\
                                <div style="position:absolute;top:0px;right:0px;font-size:9pt"><a id="qc-close" title="Đóng lại" href="">&nbsp;</a></div> \
                                <div id="float-flash-content"></div> \
                            </div>';

        var iebrws = document.all;
        _cssFixedSupport = !iebrws || iebrws && document.compatMode == "CSS1Compat" && window.XMLHttpRequest;

        _control = $('#' + _opts.controlID);
        if (_control.length <= 0) {
            _control = $('<div id="' + _opts.controlID + '">' + _opts.controlHTML + '</div>')
		    .css({ position: _cssFixedSupport ? 'fixed' : 'absolute', bottom: _opts.offsetY, right: _opts.offsetX, cursor: 'pointer' });

            $(document.body).append(_control);
        }

        _control.find('.qc-overlay').click(function() { window.open(_opts.url); });
        _control.find('#qc-close').click(function(e) {
            _close();
            e.preventDefault();
        });

        if (document.all && !window.XMLHttpRequest && mainobj.$control.text() != '')
            _control.css({ width: _control.width() });

        swfobject.embedSWF(_opts.swfFile, 'float-flash-content', _opts.width, _opts.height, '9.0.0', {}, {}, { quality: 'high', wmode: 'transparent' });

        _adjustContainer();

        $(_window).bind('scroll resize', function(e) {
            _adjustContainer();
        });
    };

    var _close = function() {
        var date = new Date();
        date.setTime(date.getTime() + (30 * 24 * 60 * 60 * 1000));
        $.cookie(_opts.cookie_name, 1, { expires: date, path: '/' });

        _control.remove();
        $(_window).unbind('scroll resize');
    };

    var _canShow = function() {
        var cookieValue = $.cookie(_opts.cookie_name);
        if (cookieValue != null && cookieValue == 1) {
            return false;
        }
        else {
            return true;
        }
    };

    return {
        init: function() {
            _init();
        },

        close: function() {
            _close();
        }
    }
};


Giamua.TopicEditDialog = function(id) {
    var dialog = new Giamua.SimpleDialog({ width: 580, height: 320, position: 'fixed' });

    var _content = '';

    var _show = function(id) {
        dialog.resetSize();
        _setContent(id);
        dialog.setContent(_content);
        dialog.show();
    };

    var _setContent = function(id) {
        _content = '<div style="background-color:#fff;"> \
                            <iframe width="560px" height="300px" src="/cs/topic_edit.aspx?id=' + id + '" frameBorder="0" scrolling="no"></iframe> \
                            <div class="action-bar"><a href="javascript:void(0);" class="close"><img src="/images/close-icon.gif" alt="Đóng popup" style="border: 0px;"/></a></div> \
                        </div>';
    };

    var _hide = function() {
        dialog.hide();
    };

    return {
        show: function(id) {
            _show(id);
        },

        close: function() {
            _hide();
        }
    }
};

Giamua.AnswerEditDialog = function() {
    var dialog = new Giamua.SimpleDialog({ width: 580, height: 320, position: 'fixed' });

    var _content = '';

    var _show = function(id) {
        dialog.resetSize();
        _setContent(id);
        dialog.setContent(_content);
        dialog.show();
    };

    var _setContent = function(id) {
        _content = '<div style="background-color:#fff;"> \
                            <iframe width="560px" height="300px" src="/cs/answer_edit.aspx?id=' + id + '" frameBorder="0" scrolling="no"></iframe> \
                            <div class="action-bar"><a href="javascript:void(0);" class="close"><img src="/images/close-icon.gif" alt="Đóng popup" style="border: 0px;"/></a></div> \
                        </div>';
    };

    var _hide = function() {
        dialog.hide();
    };

    return {
        show: function(id) {
            _show(id);
        },

        close: function() {
            _hide();
        }
    }
};

Giamua.AvatarDialog = function() {
    var dialog = new Giamua.SimpleDialog({ width: 480, height: 200, position: 'fixed' });

    var _content = '';

    var _show = function(id) {
        dialog.resetSize();
        _setContent(id);
        dialog.setContent(_content);
        dialog.show();
    };

    var _setContent = function(id) {
        _content = '<div style="background-color:#fff;"> \
                            <iframe width="460px" height="180px" src="/cs/change_avatar.aspx?id=' + id + '" frameBorder="0" scrolling="no"></iframe> \
                            <div class="action-bar"><a href="javascript:void(0);" class="close"><img src="/images/close-icon.gif" alt="Đóng popup" style="border: 0px;"/></a></div> \
                        </div>';
    };

    var _hide = function() {
        dialog.hide();
    };

    return {
        show: function(id) {
            _show(id);
        },

        close: function() {
            _hide();
        }
    }
};

